import requests
import pandas as pd
import time
import json

cookies = {
    '_uuid': '800F8EDA-6CE4-3B80-E03A-34BC6C39FC5375567infoc',
    'buvid3': '1739B47B-95EE-4CC2-886F-238ACEC17E2F13419infoc',
    'rpdid': '|(um|JuuJ|km0J\'uYkllukJmR',
    'LIVE_BUVID': 'AUTO4316353398004134',
    'video_page_version': 'v_old_home',
    'i-wanna-go-back': '-1',
    'buvid4': '0E885099-D1A6-6C82-98EF-8F67EF9DC18541426-022012419-C02VpBTfhKVU5gVKBIwePw%3D%3D',
    'buvid_fp_plain': 'undefined',
    'CURRENT_BLACKGAP': '0',
    'nostalgia_conf': '-1',
    'is-2022-channel': '1',
    'b_ut': '5',
    'SESSDATA': '0a825acc%2C1664610649%2C59403%2A41',
    'bili_jct': 'a778e31b5724f9b0347172e10935e354',
    'DedeUserID': '282632700',
    'DedeUserID__ckMd5': '37dbb587dd86ee29',
    'sid': 'iywfafib',
    'hit-dyn-v2': '1',
    'blackside_state': '0',
    'CURRENT_QUALITY': '116',
    'fingerprint3': '50f85e399eaf65ee89b6e1a1878f7587',
    'bsource': 'search_baidu',
    'fingerprint': '4e733755401f3c6c790622aced78f5ed',
    'buvid_fp': 'c679d869e93ddc1d7325d39a161ab4a2',
    'innersign': '1',
    'PVID': '2',
    'CURRENT_FNVAL': '4048',
    'bp_video_offset_282632700': '668911976393474000',
    'b_lsid': '3966943B_1815CDC8DA9',
    'b_timer': '%7B%22ffp%22%3A%7B%22333.967.fp.risk_1739B47B%22%3A%22180D2FAABF3%22%2C%22888.63804.fp.risk_1739B47B%22%3A%22180D271C332%22%2C%22444.8.fp.risk_1739B47B%22%3A%22180D2FAB30E%22%2C%22888.2421.fp.risk_1739B47B%22%3A%2218132AD241C%22%2C%22777.5.0.0.fp.risk_1739B47B%22%3A%221813482F587%22%2C%22333.1024.fp.risk_1739B47B%22%3A%2218108EF1776%22%2C%22333.1037.fp.risk_1739B47B%22%3A%22181134F5064%22%2C%22333.1007.fp.risk_1739B47B%22%3A%221813238F336%22%2C%22333.999.fp.risk_1739B47B%22%3A%221815CDC9221%22%2C%22333.788.fp.risk_1739B47B%22%3A%221813CF6A1AA%22%2C%22333.22.fp.risk_1739B47B%22%3A%22181155F6142%22%2C%22333.885.fp.risk_1739B47B%22%3A%221811D4F6C44%22%2C%22666.9.fp.risk_1739B47B%22%3A%22180FA31390B%22%2C%22666.22.fp.risk_1739B47B%22%3A%22180FA3147E1%22%2C%22444.41.fp.risk_1739B47B%22%3A%221810A3E5046%22%2C%22333.976.fp.risk_1739B47B%22%3A%221813F08EE4D%22%2C%22333.171.academy.0.fp.risk_1739B47B%22%3A%221810A978C7E%22%2C%22333.337.fp.risk_1739B47B%22%3A%22181363F4CEF%22%2C%22333.940.fp.risk_1739B47B%22%3A%221811AD7D24A%22%2C%22666.25.fp.risk_1739B47B%22%3A%221811A127DD2%22%7D%7D',
}

headers = {
    'authority': 'api.bilibili.com',
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
    # Requests sorts cookies= alphabetically
    # 'cookie': '_uuid=800F8EDA-6CE4-3B80-E03A-34BC6C39FC5375567infoc; buvid3=1739B47B-95EE-4CC2-886F-238ACEC17E2F13419infoc; rpdid=|(um|JuuJ|km0J\'uYkllukJmR; LIVE_BUVID=AUTO4316353398004134; video_page_version=v_old_home; i-wanna-go-back=-1; buvid4=0E885099-D1A6-6C82-98EF-8F67EF9DC18541426-022012419-C02VpBTfhKVU5gVKBIwePw%3D%3D; buvid_fp_plain=undefined; CURRENT_BLACKGAP=0; nostalgia_conf=-1; is-2022-channel=1; b_ut=5; SESSDATA=0a825acc%2C1664610649%2C59403%2A41; bili_jct=a778e31b5724f9b0347172e10935e354; DedeUserID=282632700; DedeUserID__ckMd5=37dbb587dd86ee29; sid=iywfafib; hit-dyn-v2=1; blackside_state=0; CURRENT_QUALITY=116; fingerprint3=50f85e399eaf65ee89b6e1a1878f7587; bsource=search_baidu; fingerprint=4e733755401f3c6c790622aced78f5ed; buvid_fp=c679d869e93ddc1d7325d39a161ab4a2; innersign=1; PVID=2; CURRENT_FNVAL=4048; bp_video_offset_282632700=668911976393474000; b_lsid=3966943B_1815CDC8DA9; b_timer=%7B%22ffp%22%3A%7B%22333.967.fp.risk_1739B47B%22%3A%22180D2FAABF3%22%2C%22888.63804.fp.risk_1739B47B%22%3A%22180D271C332%22%2C%22444.8.fp.risk_1739B47B%22%3A%22180D2FAB30E%22%2C%22888.2421.fp.risk_1739B47B%22%3A%2218132AD241C%22%2C%22777.5.0.0.fp.risk_1739B47B%22%3A%221813482F587%22%2C%22333.1024.fp.risk_1739B47B%22%3A%2218108EF1776%22%2C%22333.1037.fp.risk_1739B47B%22%3A%22181134F5064%22%2C%22333.1007.fp.risk_1739B47B%22%3A%221813238F336%22%2C%22333.999.fp.risk_1739B47B%22%3A%221815CDC9221%22%2C%22333.788.fp.risk_1739B47B%22%3A%221813CF6A1AA%22%2C%22333.22.fp.risk_1739B47B%22%3A%22181155F6142%22%2C%22333.885.fp.risk_1739B47B%22%3A%221811D4F6C44%22%2C%22666.9.fp.risk_1739B47B%22%3A%22180FA31390B%22%2C%22666.22.fp.risk_1739B47B%22%3A%22180FA3147E1%22%2C%22444.41.fp.risk_1739B47B%22%3A%221810A3E5046%22%2C%22333.976.fp.risk_1739B47B%22%3A%221813F08EE4D%22%2C%22333.171.academy.0.fp.risk_1739B47B%22%3A%221810A978C7E%22%2C%22333.337.fp.risk_1739B47B%22%3A%22181363F4CEF%22%2C%22333.940.fp.risk_1739B47B%22%3A%221811AD7D24A%22%2C%22666.25.fp.risk_1739B47B%22%3A%221811A127DD2%22%7D%7D',
    'origin': 'https://space.bilibili.com',
    'referer': 'https://space.bilibili.com/928123/video?tid=0&page=3&keyword=&order=pubdate',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
}

# 存储所有信息的列表 方便等下直接使用 pandas 转换为 csv 文件
data_all = [] 

# 遍历列表 否则无法进行翻页
for i in range(1,11):

    # 修改这个里面的pn 就是page 就可以实现翻页了
    params = {
        'mid': '928123',
        'ps': '30',
        'tid': '0',
        'pn': i,
        'keyword': '',
        'order': 'pubdate',
        'jsonp': 'jsonp',
    }


    response = requests.get('https://api.bilibili.com/x/space/arc/search', params=params, cookies=cookies, headers=headers)

    # 请求到的json数据格式要进行转换才能提取到我们想要的信息
    con = json.loads(response.text)
    # 定位到我们想要的信息
    lits_data = con['data']['list']['vlist']
    # print(lits_data)
    # 遍历该页的数据列表
    for i in lits_data:
        # 遍历之后提取每一个番剧的具体信息

        time2 = i['created'] # 日期
        #转换成localtime
        time_local = time.localtime(time2)
        #转换成新的时间格式(2016-05-05 20:28:54)
        time2 = time.strftime("%Y-%m-%d %H:%M:%S", time_local)

        title = i['title']# 标题
        play = i['play']# 播放量
        comment = i['comment']# 评论数

        print([time2, title, play, comment])
        data_all.append([time2, title, play, comment])
df = pd.DataFrame(data_all, columns=['发布日期', '标题', '播放量', '评论量'])
df.to_csv('B站番剧数据(1.csv')